<?php

namespace app\api\controller;


use Common;
use think\facade\Db;
use think\Response;
use think\Request;

class MeiTuanSession extends Wap
{
    /**
     *
     *  美团更新session 每28-30天执行一次
     */

    public function index()
    {

        $tiem = time() - 31 * 86400;

        $list = DB::name("customer_meituan_config")->where('update_time', '>', $tiem)->select();


        $url = "https://openapi.dianping.com/router/oauth/token";
        $header = array(
            'Content-Type:' . 'application/x-www-form-urlencoded; charset=UTF-8'
        );
        if (isset($list)) {
            foreach ($list as $key => $val) {
                if ($val['refresh_token'] != "") {
                    if (isset($val['update_time'])) {

                        if ($val['update_time'] > 0) {
                            if (($val['update_time'] + 29 * 86400) < time()) {

                                $data = array();
                                $data['app_key'] = $val['appkey'];
                                $data['app_secret'] = $val['secret'];
                                $data['grant_type'] = 'refresh_token';
                                $data['refresh_token'] = $val['refresh_token'];


                                $res = $this->meituan_curl_post($url, $data, $header);
                                $return = json_decode($res, true);

                                //记录日志 勿删
                                if ($return['code'] == 200) {
                                    $update = array();
                                    $update['session'] = $return['access_token'];
                                    $update['refresh_token'] = $return['refresh_token'];

                                    $update['session_update_time'] = time();
                                    $update['update_time'] = time();
                                    $update['expires_in'] = $return['expires_in'];
                                    DB::name('customer_meituan_config')->where(['appkey' => $val['appkey']])->update($update);

                                }

                            }
                        }
                    } else {
                        $data = array();
                        $data['app_key'] = $val['appkey'];
                        $data['app_secret'] = $val['secret'];
                        $data['grant_type'] = 'refresh_token';
                        $data['refresh_token'] = $val['refresh_token'];


                        $res = $this->meituan_curl_post($url, $data, $header);
                        $return = json_decode($res, true);

                        //记录日志 勿删
                        if ($return['code'] == 200) {
                            $update = array();
                            $update['session'] = $return['access_token'];
                            $update['refresh_token'] = $return['refresh_token'];

                            $update['session_update_time'] = time();
                            $update['update_time'] = time();
                            $update['expires_in'] = $return['expires_in'];
                            DB::name('customer_meituan_config')->where(['appkey' => $val['appkey']])->update($update);

                        }
                    }


                } else {
                    $data = array();
                    $data['app_key'] = $val['appkey'];
                    $data['app_secret'] = $val['secret'];
                    $data['grant_type'] = 'refresh_token';
                    $data['refresh_token'] = $val['refresh_token'];


                    $res = $this->meituan_curl_post($url, $data, $header);
                    $return = json_decode($res, true);

                    //记录日志 勿删
                    if ($return['code'] == 200) {
                        $update = array();
                        $update['session'] = $return['access_token'];
                        $update['refresh_token'] = $return['refresh_token'];

                        $update['session_update_time'] = time();
                        $update['update_time'] = time();
                        $update['expires_in'] = $return['expires_in'];
                        DB::name('customer_meituan_config')->where(['appkey' => $val['appkey']])->update($update);

                    }
                }


            }
        }

    }

    /**
     *   请求点评管家
     */
    public function meituan_curl_post($url, $data, $header)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        $result = curl_exec($ch);
        curl_close($ch);

        return $result;
    }



}













